﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using MINERP.MODEL.Frontier;
using MINERP.MODEL.FoundationModel;
using MINERP.COM;
using MINERP.BLL.Frontier;

using FastReport;
using MINERP.BLL.Common;

namespace MINERP.Frontier
{
    public partial class FrmConfig : Form
    {
        private PaperBoxBLL paperBoxBLL = new PaperBoxBLL();
        private PaperConfigRecordBLL paperConfigRecordBLL = new PaperConfigRecordBLL();
        PrinterConfigInfo _printerConfigInfo = null;
        private Printer_Info printerInfo = null;

        public RefreshStatusHander refreshStatusHander;

        
        /// <summary>
        /// 打印机
        /// </summary>
        public Printer_Info PrinterInfo
        {
            set { printerInfo = value; }
        }


        public delegate void ChangePaperCompleteHander();
        public event ChangePaperCompleteHander ChangePaperCompleteEvent;

        public FrmConfig()
        {
            InitializeComponent();

        }

        private void FrmConfig_Load(object sender, EventArgs e)
        {
            LinkConfigHistory();
            LinkPapeType();
            //LinkPaperFace();
            LinkPaperBox();
            GetPrinterConfig();
        }

        /// <summary>
        /// 获得打印机其他设置
        /// </summary>
        private void GetPrinterConfig()
        {
            PrintConfigBLL bll = new PrintConfigBLL();
            _printerConfigInfo = bll.GetPrinterConfigInfo(printerInfo.PrinterID);
            if (_printerConfigInfo != null)
            {
                txtPrinterGridNum.Text = _printerConfigInfo.PrinterGridNum.ToString();
                chkIsAutoPrintTicket.Checked = _printerConfigInfo.IsAutoPrintTikect;
                FrmMain._printerConfigInfo = _printerConfigInfo;
            }
        }
        private void btnOk_Click(object sender, EventArgs e)
        {
            try
            {
                int ret;
                AddPaperConfigRecord();
                ret = paperBoxBLL.UpdatePaperBox(cmbPaperBox.Text, printerInfo.PrinterID, cmbPaperType.Text, Convert.ToDouble(txtPaperWidth.Text.Trim()));
                if (ret == 0)
                {
                    ChangePaperCompleteEvent.Invoke();
                    this.Close();
                }
                LinkConfigHistory();
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message,"提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
            }
        }
        /// <summary>
        /// 添加纸设置信息
        /// </summary>
        private void AddPaperConfigRecord()
        {
            int ret = paperConfigRecordBLL.AddPaperConfigRecord(cmbPaperType.Text, Convert.ToDouble(txtPaperWidth.Text.Trim()));
            if (ret != 0)
            {
                MessageBox.Show("数据库连接失败","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
            }
        }
        private bool DataCheck()
        {
            bool flag = false;

            return flag;
 
        }
        /// <summary>
        /// 绑定设置历史记录
        /// </summary>
        private void LinkConfigHistory()
        {
            gcPrinterConfig.DataSource = paperConfigRecordBLL.GetPaperConfigRecordList();
            gvPrinterConfig.RefreshData();
        }

        /// <summary>
        /// 绑定纸型
        /// </summary>
        private void LinkPapeType()
        {
            cmbPaperType.Properties.Items.Clear();
            foreach (var item in PaperTypeBLL.GetAllPaperType())
            {
                cmbPaperType.Properties.Items.Add(item.Name);
            }
            cmbPaperType.SelectedIndex = 0;
        }

        /// <summary>
        /// 绑定纸面
        /// </summary>
        //private void LinkPaperFace()
        //{

        //    cmbPaperFace.Properties.Items.Clear();
        //    foreach (var item in PaperFaceBLL.GetAllPaperFace())
        //    {
        //        cmbPaperFace.Properties.Items.Add(item.Name);
        //    }
        //    cmbPaperFace.SelectedIndex = 0;
        //}
        /// <summary>
        /// 绑定纸箱
        /// </summary>
        private void LinkPaperBox()
        {
            cmbPaperBox.Properties.Items.Clear();
            foreach (var item in paperBoxBLL.GetPaperBoxList(printerInfo.PrinterID))
            {
                cmbPaperBox.Properties.Items.Add(item.PaperBoxName);
            }
            cmbPaperBox.SelectedIndex = 0;
        }

        private void gvPrinterConfig_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            PaperConfigRecordInfo pc = (PaperConfigRecordInfo)gvPrinterConfig.GetFocusedRow();
            cmbPaperType.Text = pc.PaperTypeName;
            //cmbPaperFace.Text = pc.PaperFaceName;
            txtPaperWidth.Text = pc.PaperWidth.ToString();

        }

        private void btnOrtherConfigOK_Click(object sender, EventArgs e)
        {
            PrintConfigBLL bll = new PrintConfigBLL();
            if (!string.IsNullOrEmpty(txtPrinterGridNum.Text.Trim()))
            {
               int ret = bll.UpdatePrinterConfig(printerInfo.PrinterID, chkIsAutoPrintTicket.Checked, Convert.ToInt32(txtPrinterGridNum.Text));
               if (ret == 0)
               {
                   _printerConfigInfo = new PrinterConfigInfo();
                   _printerConfigInfo.PrinterGridNum = Convert.ToInt32(txtPrinterGridNum.Text);
                   _printerConfigInfo.IsAutoPrintTikect = chkIsAutoPrintTicket.Checked;
                   _printerConfigInfo.PrinterID = printerInfo.PrinterID;
                   FrmMain._printerConfigInfo = _printerConfigInfo;
                   refreshStatusHander.Invoke();
                   //MessageBox.Show("保存设置成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                   this.Close();
               }
               else
               {
                   MessageBox.Show("连接数据库失败", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
               }
            }
            else
            {
                MessageBox.Show("请输入打印机格子数","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
            }
        }

        private void chkIsAutoPrintTicket_CheckedChanged(object sender, EventArgs e)
        {

        }



    }
}
